package com.topcent.tioxyc.consumer;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.rabbitmq.client.Channel;

import com.topcent.tioxyc.constants.KeyConstant;
import com.topcent.tioxyc.pojo.ActualCostVO;
import com.topcent.tioxyc.pojo.Assignee;
import com.topcent.tioxyc.pojo.CostBudget;
import com.topcent.tioxyc.pojo.vo.*;
import com.topcent.tioxyc.util.ApiClient;
import com.topcent.tioxyc.util.ConnmeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: 陈百万
 * @date: 2025/11/12 14:28
 * @Description: 费用单创建数据就添加到固定费、变动费
 */
@Component
@RabbitListener(queues = "feiYongAddCost")
@Slf4j
public class CcFeiYongAddCostConsumer {

    private static final String DATA_CREATE = "data_create";

    @RabbitHandler
    public void handleMessage(String msg, Channel channel, Message message) throws IOException {
        try {
            // 在这里处理推送来的数据
            JSONObject payloadJSON = JSONUtil.parseObj(msg);
            String op = payloadJSON.getStr("op");
            String data = payloadJSON.getStr("data");

            if (DATA_CREATE.equals(op)) {
                create(data);
            }

            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            System.out.println("Message acknowledged");
        } catch (Exception e) {
            // 处理异常并拒绝消息
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
            System.err.println("发送消息失败，重试中 " + e.getMessage());
        }
    }

    private void create(String datas){
        int flag = 0;
        Header[] headers = ConnmeUtils.createHeaders();
        CostOrderFeeVO costOrderFeeVO = JSONUtil.toBean(datas, CostOrderFeeVO.class);

        String appId = "665fbebb30a77df3f2d3d47d";
        String entryId = "68b8f8bad231151e6cc8a785"; // 预算表格明细数据

        List<CostBudget> costBudgets = new ArrayList<>();
        Map<String, Object> parameter = new HashMap<>();
        parameter.put("app_id", appId);
        parameter.put("entry_id", entryId);
        boolean fale = true;
        String zh = "";
        do {
            if (!zh.isEmpty()) {
                parameter.put("data_id", zh);
            }
            parameter.put("limit", 100);
            String jsonSale = JSONUtil.toJsonStr(parameter);
            JSONObject jdySale = ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/app/entry/data/list", headers, jsonSale);
            String data = jdySale.getStr("data");
            JSONArray objects = JSONUtil.parseArray(data);
            List<CostBudget> list = JSONUtil.toList(objects, CostBudget.class);
            if (list.size() == 100) {
                zh = list.getLast().get_id();
            } else {
                fale = false;
            }
            costBudgets.addAll(list);
        } while (fale);

        // 固定费  分组
        Map<String, List<FixedBudgetVO>> fixededBudgetVOList = fixedBudgetVOList(headers);
        // 变动费  分组
        Map<String, List<VariableCostVO>> variableCostVOList = variableCostVOList(headers);

        Map<String, List<ActualCostVO>> actualCostVOList = actualCostVOList(headers);

        // 核心逻辑：先匹配科目编码，再判断费用类型
        List<CostOrderFeeVO.payInfo> payInfoList = costOrderFeeVO.get_widget_1717552286301();
        if (payInfoList == null || payInfoList.isEmpty()) {
            return;
        }

        // 遍历费用单的每个子数据（payInfo）
        for (CostOrderFeeVO.payInfo payInfo : payInfoList) {
            int i = 0;
            String currentSubject = payInfo.get_widget_1721007075246(); // 当前子数据的总账科目
            String deptNameZ = payInfo.get_widget_1721461935882(); // 费用归属部门
            if (currentSubject == null || currentSubject.trim().isEmpty()) {
                continue;
            }

            // 遍历预算数据，寻找科目编码匹配的项
            for (CostBudget budget : costBudgets) {
                String budgetSubject = budget.get_widget_1756952762307(); // 预算的科目编码
                // 关键匹配条件：科目编码非空且与当前子数据的总账科目完全一致
                if (budgetSubject != null && budgetSubject.equals(currentSubject)) {
                    // 仅在匹配成功后，才判断费用类型
                    String feeType = budget.get_widget_1756952762313();
                    if ("固定费".equals(feeType)) {
                        //String deptName = budget.get_widget_1756952762311().getName();
                        String groupKey = budgetSubject + "_" + deptNameZ;
                        log.info("费用单预算固定费测试：{}", costOrderFeeVO.get_widget_1717550819156()+ "===" + groupKey);
                        if(fixededBudgetVOList.containsKey(groupKey)){
                            FixedBudgetVO first = fixededBudgetVOList.get(groupKey).getFirst();
                            BigDecimal bigDecimal = payInfo.get_widget_1717568393615().abs();  // 金额
                            first.set_widget_1756954464270(bigDecimal.add(first.get_widget_1756954464270()));

                            // 获取累计预算金额和累计实际金额
                            BigDecimal budgetAmount = first.get_widget_1756953937800();
                            BigDecimal actualAmount = first.get_widget_1756954464270();

                            // 计算累计差额（处理空值情况，默认空值为0）
                            BigDecimal difference;
                            if (budgetAmount == null && actualAmount == null) {
                                difference = BigDecimal.ZERO;
                            } else if (budgetAmount == null) {
                                difference = actualAmount.negate(); // 预算为null，差额为-实际金额
                            } else if (actualAmount == null) {
                                difference = budgetAmount; // 实际为null，差额为预算金额
                            } else {
                                difference = budgetAmount.subtract(actualAmount); // 正常计算：预算-实际
                            }

                            // 设置累计差额
                            payInfo.set_widget_1758768525616(difference);

                            payInfo.set_widget_1757905260882(budgetAmount); // 累计预算金额
                            payInfo.set_widget_1757905260883(actualAmount); // 累计实际金额

                            String isOverBudget;
                            // BigDecimal比较：用compareTo()，返回1表示actual>budget，-1表示actual<budget，0表示相等
                            int compareResult = actualAmount.compareTo(budgetAmount);
                            if (compareResult > 0) {
                                isOverBudget = "已超预算"; // 实际金额 > 预算金额 → 超预算
                            } else {
                                isOverBudget = "未超预算"; // 实际金额 ≤ 预算金额 → 未超预算
                            }
                            payInfo.set_widget_1757905260881(isOverBudget); // 是否超预算


                            FixedBudgetResult result = new FixedBudgetResult();
                            result.set_id(first.get_id());
                            result.set_widget_1756954464270(first.get_widget_1756954464270());

                            Map<String, Object> parameterEnd = new HashMap<>();
                            parameterEnd.put("app_id", "665fbebb30a77df3f2d3d47d");
                            parameterEnd.put("entry_id", "68b8fd53800c31f0bf6131f5"); // 固定费
                            parameterEnd.put("is_start_trigger", "true");
                            parameterEnd.put("data_id", result.get_id());
                            result.set_id(null);
                            parameterEnd.put("data", ConnmeUtils.convertEntityToMapNull(result));
                            String jsonSaleEnd = JSONUtil.toJsonStr(parameterEnd);
                            ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/app/entry/data/update", headers, jsonSaleEnd);
                            flag = 1;i = 1;
                        }

                    } else if ("变动费".equals(feeType)) {
                        //String deptName = budget.get_widget_1756952762308().getName();
                        String groupKey = budgetSubject + "_" + deptNameZ;
                        log.info("费用单预算变动费测试：{}", costOrderFeeVO.get_widget_1717550819156()+ "===" + groupKey);
                        if(variableCostVOList.containsKey(groupKey)){
                            VariableCostVO first = variableCostVOList.get(groupKey).getFirst();
                            BigDecimal bigDecimal = payInfo.get_widget_1717568393615().abs();  // 金额
                            BigDecimal bigDecimal1 = first.get_widget_1756955436117();
                            first.set_widget_1756954464270(bigDecimal.add(first.get_widget_1756954464270()));

                            // 获取累计预算金额和累计实际金额
                            BigDecimal budgetAmount = first.get_widget_1756953937800();
                            BigDecimal actualAmount = first.get_widget_1756954464270();

                            // 计算累计差额（处理空值情况，默认空值为0）
                            BigDecimal difference;
                            if (budgetAmount == null && actualAmount == null) {
                                difference = BigDecimal.ZERO;
                            } else if (budgetAmount == null) {
                                difference = actualAmount.negate(); // 预算为null，差额为-实际金额
                            } else if (actualAmount == null) {
                                difference = budgetAmount; // 实际为null，差额为预算金额
                            } else {
                                difference = budgetAmount.subtract(actualAmount); // 正常计算：预算-实际
                            }

                            // 设置累计差额
                            payInfo.set_widget_1758768525616(difference);

                            payInfo.set_widget_1757905260882(budgetAmount); // 累计预算金额
                            payInfo.set_widget_1757905260883(actualAmount); // 累计实际金额
                            String isOverBudget;
                            // BigDecimal比较：用compareTo()，返回1表示actual>budget，-1表示actual<budget，0表示相等
                            int compareResult = actualAmount.compareTo(budgetAmount);
                            if (compareResult > 0) {
                                isOverBudget = "已超预算"; // 实际金额 > 预算金额 → 超预算
                            } else {
                                isOverBudget = "未超预算"; // 实际金额 ≤ 预算金额 → 未超预算
                            }
                            payInfo.set_widget_1757905260881(isOverBudget); // 是否超预算

                            BigDecimal field118 = BigDecimal.ZERO; // 默认值设为0（可根据业务调整，如null）

                            // 校验：字段不为null + 字段不为0（避免空指针和除数为0）
                            if (bigDecimal1 != null && BigDecimal.ZERO.compareTo(bigDecimal1) != 0) {
                                // 除法计算：保留5位小数（根据业务需求调整），四舍五入（避免无限小数）
                                field118 = first.get_widget_1756954464270().divide(bigDecimal1, 5, RoundingMode.HALF_UP);
                            }
                            // 赋值118字段
                            first.set_widget_1756955436118(field118);

                            VariableCostResult result = new VariableCostResult();
                            result.set_id(first.get_id());
                            result.set_widget_1756954464270(first.get_widget_1756954464270());
                            result.set_widget_1756955436118(field118);

                            Map<String, Object> parameterEnd = new HashMap<>();
                            parameterEnd.put("app_id", "665fbebb30a77df3f2d3d47d");
                            parameterEnd.put("entry_id", "68b9031c198a79cfab10ec74"); // 变动费
                            parameterEnd.put("is_start_trigger", "true");
                            parameterEnd.put("data_id", result.get_id());
                            result.set_id(null);
                            parameterEnd.put("data", ConnmeUtils.convertEntityToMapNull(result));
                            String jsonSaleEnd = JSONUtil.toJsonStr(parameterEnd);
                            ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/app/entry/data/update", headers, jsonSaleEnd);

                            flag = 1; i = 1;
                        }
                    }
                    break; // 找到匹配项后，跳出预算循环（避免重复匹配）
                }
            }

            // 累计实际费用表单
            String groupKey = currentSubject + "_" + payInfo.get_widget_1721461935882()+ "_" + costOrderFeeVO.get_widget_1737096027488().getName();
            if(actualCostVOList.containsKey(groupKey)){
                ActualCostVO first = actualCostVOList.get(groupKey).getFirst();
                BigDecimal bigDecimal = payInfo.get_widget_1717568393615().abs();  // 这次费用金额
                BigDecimal addMoney = first.get_widget_1758766069065(); // 累计实际费用金额
                first.set_widget_1758766069065(bigDecimal.add(addMoney)); // 新累计实际费用金额
                if(i == 0){
                    // 设置累计差额
                    payInfo.set_widget_1758768525616(first.get_widget_1758766069065());
                    payInfo.set_widget_1757905260882(BigDecimal.ZERO); // 累计预算金额
                    payInfo.set_widget_1757905260883(first.get_widget_1758766069065()); // 累计实际金额
                    payInfo.set_widget_1757905260881("已超预算"); // 是否超预算
                }
                ActualCostR actualCostR = new ActualCostR();
                actualCostR.set_id(first.get_id());
                actualCostR.set_widget_1758766069065(first.get_widget_1758766069065()); // 金额

                Map<String, Object> parameterEnd = new HashMap<>();
                parameterEnd.put("app_id", "665fbebb30a77df3f2d3d47d");
                parameterEnd.put("entry_id", "68d4a3f561fd393b761f73ed");
                parameterEnd.put("is_start_trigger", "true");
                parameterEnd.put("data_id", actualCostR.get_id());
                actualCostR.set_id(null);
                parameterEnd.put("data", ConnmeUtils.convertEntityToMapNull(actualCostR));
                String jsonSaleEnd = JSONUtil.toJsonStr(parameterEnd);
                ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/app/entry/data/update", headers, jsonSaleEnd);

                flag = 1;
            }
        }
        if(flag == 1){
            // 更新简道云的费用单的预算金额是否超预期字段
            ExpenseSheetSonResult expenseSheetSonResult = manualConvert(costOrderFeeVO);
            Map<String, Object> parameterEnd = new HashMap<>();
            parameterEnd.put("app_id", "665fbebb30a77df3f2d3d47d");
            parameterEnd.put("entry_id", "665fbee388d8faa4b491f649");
            parameterEnd.put("is_start_trigger", "true");
            parameterEnd.put("data_id", expenseSheetSonResult.get_id());
            expenseSheetSonResult.set_id(null);
            parameterEnd.put("data", ConnmeUtils.convertEntityToMapNull(expenseSheetSonResult));
            String jsonSaleEnd = JSONUtil.toJsonStr(parameterEnd);
            ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/app/entry/data/update", headers, jsonSaleEnd);
            log.info("【关键收尾】费用单最终更新成功-费用单号={}", costOrderFeeVO.get_widget_1717550819156());
        }
    }


    // 简道云固定费的全部数据
    private Map<String, List<FixedBudgetVO>> fixedBudgetVOList(Header[] headers){
        String appId = "665fbebb30a77df3f2d3d47d";
        String entryId = "68b8fd53800c31f0bf6131f5";

        List<FixedBudgetVO> costBudgets = new ArrayList<>();
        Map<String, Object> parameter = new HashMap<>();
        parameter.put("app_id", appId);
        parameter.put("entry_id", entryId);
        boolean fale = true;
        String zh = "";
        do {
            if (!zh.isEmpty()) {
                parameter.put("data_id", zh);
            }
            parameter.put("limit", 100);
            String jsonSale = JSONUtil.toJsonStr(parameter);
            JSONObject jdySale = ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/app/entry/data/list", headers, jsonSale);
            String data = jdySale.getStr("data");
            JSONArray objects = JSONUtil.parseArray(data);
            List<FixedBudgetVO> list = JSONUtil.toList(objects, FixedBudgetVO.class);
            if (list.size() == 100) {
                zh = list.getLast().get_id();
            } else {
                fale = false;
            }
            costBudgets.addAll(list);
        } while (fale);

        // 按"科目编码_部门名称"分组
        Map<String, List<FixedBudgetVO>> groupedMap = new HashMap<>();
        for (FixedBudgetVO budget : costBudgets) {
            // 获取分组关键字段
            String subjectCode = budget.get_widget_1756953937797();
            String deptName = budget.get_widget_1756953937802().getName();

            // 跳过科目编码为空的数据（避免空键）
            if (subjectCode == null || subjectCode.trim().isEmpty()) {
                continue;
            }

            // 生成分组键（格式：科目编码_部门名称）
            String groupKey = subjectCode + "_" + deptName;

            // 将数据添加到对应分组
            if (!groupedMap.containsKey(groupKey)) {
                groupedMap.put(groupKey, new ArrayList<>());
            }
            groupedMap.get(groupKey).add(budget);
        }

        return groupedMap;
    }


    // 简道云变动费的全部数据
    private Map<String, List<VariableCostVO>> variableCostVOList(Header[] headers){
        String appId = "665fbebb30a77df3f2d3d47d";
        String entryId = "68b9031c198a79cfab10ec74";

        List<VariableCostVO> costBudgets = new ArrayList<>();
        Map<String, Object> parameter = new HashMap<>();
        parameter.put("app_id", appId);
        parameter.put("entry_id", entryId);
        boolean fale = true;
        String zh = "";
        do {
            if (!zh.isEmpty()) {
                parameter.put("data_id", zh);
            }
            parameter.put("limit", 100);
            String jsonSale = JSONUtil.toJsonStr(parameter);
            JSONObject jdySale = ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/app/entry/data/list", headers, jsonSale);
            String data = jdySale.getStr("data");
            JSONArray objects = JSONUtil.parseArray(data);
            List<VariableCostVO> list = JSONUtil.toList(objects, VariableCostVO.class);
            if (list.size() == 100) {
                zh = list.getLast().get_id();
            } else {
                fale = false;
            }
            costBudgets.addAll(list);
        } while (fale);

        // 按"科目编码_部门名称"分组
        Map<String, List<VariableCostVO>> groupedMap = new HashMap<>();
        for (VariableCostVO budget : costBudgets) {
            // 获取分组关键字段
            String subjectCode = budget.get_widget_1756953937797();
            String deptName = budget.get_widget_1756953937802().getName();

            // 跳过科目编码为空的数据（避免空键）
            if (subjectCode == null || subjectCode.trim().isEmpty()) {
                continue;
            }

            // 生成分组键（格式：科目编码_部门名称）
            String groupKey = subjectCode + "_" + deptName;

            // 将数据添加到对应分组
            if (!groupedMap.containsKey(groupKey)) {
                groupedMap.put(groupKey, new ArrayList<>());
            }
            groupedMap.get(groupKey).add(budget);
        }

        return groupedMap;
    }


    // 简道云累计实际费用费的全部数据
    private Map<String, List<ActualCostVO>> actualCostVOList(Header[] headers){
        String appId = "665fbebb30a77df3f2d3d47d";
        String entryId = "68d4a3f561fd393b761f73ed";

        List<ActualCostVO> costBudgets = new ArrayList<>();
        Map<String, Object> parameter = new HashMap<>();
        parameter.put("app_id", appId);
        parameter.put("entry_id", entryId);
        boolean fale = true;
        String zh = "";
        do {
            if (!zh.isEmpty()) {
                parameter.put("data_id", zh);
            }
            parameter.put("limit", 100);
            String jsonSale = JSONUtil.toJsonStr(parameter);
            JSONObject jdySale = ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/app/entry/data/list", headers, jsonSale);
            String data = jdySale.getStr("data");
            JSONArray objects = JSONUtil.parseArray(data);
            List<ActualCostVO> list = JSONUtil.toList(objects, ActualCostVO.class);
            if (list.size() == 100) {
                zh = list.getLast().get_id();
            } else {
                fale = false;
            }
            costBudgets.addAll(list);
        } while (fale);

        // 按"科目编码_部门名称"分组
        Map<String, List<ActualCostVO>> groupedMap = new HashMap<>();
        for (ActualCostVO budget : costBudgets) {
            // 获取分组关键字段
            String subjectCode = budget.get_widget_1758766069063();

            // 受益部门（做判空处理）
            String deptName = "";
            if (budget.get_widget_1758766069064() != null) {
                deptName = budget.get_widget_1758766069064().getName();
            }

            // 预算部门（做判空处理）
            String ysdeptName = "";
            if (budget.get_widget_1758766069067() != null) {
                ysdeptName = budget.get_widget_1758766069067().getName();
            }


            // 跳过科目编码为空的数据（避免空键）
            if (subjectCode == null || subjectCode.trim().isEmpty()) {
                continue;
            }

            // 生成分组键（格式：科目编码_部门名称）
            String groupKey = subjectCode + "_" + deptName + "_" + ysdeptName;

            // 将数据添加到对应分组
            if (!groupedMap.containsKey(groupKey)) {
                groupedMap.put(groupKey, new ArrayList<>());
            }
            groupedMap.get(groupKey).add(budget);
        }

        return groupedMap;
    }


    /**
     * 完整手动转换：CostOrderFeeVO → ExpenseSheetSonResult
     * 覆盖所有字段映射，处理Assignee转username，包含空值安全校验
     */
    public ExpenseSheetSonResult manualConvert(CostOrderFeeVO costOrderFeeVO) {
        // 1. 入参空值校验：避免后续调用get方法空指针
        if (costOrderFeeVO == null) {
            return null;
        }

        // 2. 初始化目标对象，映射外层共有字段
        ExpenseSheetSonResult result = new ExpenseSheetSonResult();
        result.set_id(costOrderFeeVO.get_id()); // 外层唯一共有字段：_id

        // 3. 处理payInfo列表：空列表安全处理 + 逐个字段映射
        List<ExpenseSheetSonResult.payInfo> resultPayList = new ArrayList<>();
        List<CostOrderFeeVO.payInfo> sourcePayList = costOrderFeeVO.get_widget_1717552286301();
        if (sourcePayList != null && !sourcePayList.isEmpty()) {
            resultPayList = sourcePayList.stream()
                    .map(sourcePay -> {
                        ExpenseSheetSonResult.payInfo resultPay = new ExpenseSheetSonResult.payInfo();

                        // -------------------------- 复制payInfo所有共有字段 --------------------------
                        // 1. 字符串类型字段
                        resultPay.set_widget_1721461935882(sourcePay.get_widget_1721461935882()); // 费用归属部门
                        resultPay.set_widget_1721007075246(sourcePay.get_widget_1721007075246()); // 总账科目
                        resultPay.set_widget_1721461935881(sourcePay.get_widget_1721461935881()); // 成本中心
                        resultPay.set_widget_1721461935883(sourcePay.get_widget_1721461935883()); // 公司名称
                        resultPay.set_widget_1721007075245(sourcePay.get_widget_1721007075245()); // 费用类型
                        resultPay.set_widget_1757401426506(sourcePay.get_widget_1757401426506()); // 索赔单链接
                        resultPay.set_widget_1726895575854(sourcePay.get_widget_1726895575854()); // 供应商编码
                        resultPay.set_widget_1726895575855(sourcePay.get_widget_1726895575855()); // 供应商名称
                        resultPay.set_widget_1724055536060(sourcePay.get_widget_1724055536060()); // 客户名称
                        resultPay.set_widget_1724055536059(sourcePay.get_widget_1724055536059()); // 客户编码
                        resultPay.set_widget_1718608144581(sourcePay.get_widget_1718608144581()); // 销售订单号
                        resultPay.set_widget_1723604759122(sourcePay.get_widget_1723604759122()); // 用途
                        resultPay.set_widget_1750146092691(sourcePay.get_widget_1750146092691()); // 是否异币种
                        resultPay.set_widget_1750146092694(sourcePay.get_widget_1750146092694()); // 收款币种
                        resultPay.set_widget_1750327632632(sourcePay.get_widget_1750327632632()); // 收款金额
                        resultPay.set_widget_1750751819118(sourcePay.get_widget_1750751819118()); // 辅助计算
                        resultPay.set_widget_1757905260881(sourcePay.get_widget_1757905260881()); // 是否超预算

                        resultPay.set_widget_1760412973469(sourcePay.get_widget_1760412973469()); // 科目名称

                        resultPay.set_widget_1721461935880(sourcePay.get_widget_1721461935880()); // 费用归属部门选择
                        resultPay.set_widget_1717568393642(sourcePay.get_widget_1717568393642()); // 选择费用类型
                        resultPay.set_widget_1760173835621(sourcePay.get_widget_1760173835621()); // 选择项目
                        resultPay.set_widget_1760173835622(sourcePay.get_widget_1760173835622()); // 项目名称
                        // -------------------------- 特殊字段处理：Assignee转username --------------------------
                        Assignee assigneej = sourcePay.get_widget_1760173835623();
                        resultPay.set_widget_1760173835623(
                                assigneej != null ? assigneej.getUsername() : null // 项目经理
                        );

                        resultPay.set_widget_1724055536058(sourcePay.get_widget_1724055536058()); // 客户选择明细
                        resultPay.set_widget_1722221874141(sourcePay.get_widget_1722221874141()); // 选择销售订单

                        // 2. BigDecimal数值类型字段
                        resultPay.set_widget_1717568393615(sourcePay.get_widget_1717568393615()); // 本币金额
                        resultPay.set_widget_1717552511795(sourcePay.get_widget_1717552511795()); // 金额
                        resultPay.set_widget_1758768525616(sourcePay.get_widget_1758768525616()); // 差额
                        resultPay.set_widget_1757905260882(sourcePay.get_widget_1757905260882()); // 累计预算费用
                        resultPay.set_widget_1757905260883(sourcePay.get_widget_1750751819118() != null ?
                                sourcePay.get_widget_1757905260883() : null); // 累计实际费用（空值兼容）

                        // -------------------------- 特殊字段处理：Assignee转username --------------------------
                        Assignee assignee = sourcePay.get_widget_1717552511787();
                        resultPay.set_widget_1717552511787(
                                assignee != null ? assignee.getUsername() : null // 避免Assignee为null时空指针
                        );

                        return resultPay;
                    })
                    .collect(Collectors.toList());
        }

        // 4. 给目标对象设置转换后的payInfo列表
        result.set_widget_1717552286301(resultPayList);

        return result;
    }


}
